package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception {
        /**
         * 根据传递过来的数据判断
         * 1.是否已经设置预约值
         * 2.预约人数是否已满
         * 3.用户是否在同一天预约了同一个套餐
         * 4.是否是会员，不是就注册
         * 预约成功之后当日的预约人数加1
         * 5预约成功之后返回id到前端
         */
        String orderDate = (String) map.get("orderDate");//预约日期
        OrderSetting orderSetting = orderSettingDao.findByDate(DateUtils.parseString2Date(orderDate));
        if(orderSetting==null){//判断当前日期是否设置了预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断人数是否已经预约满
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if(number<=reservations) return new Result(false,MessageConstant.ORDER_FULL);

        //判断当前用户用户是否是重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if(member!=null){
            //用户存在，判断是否是重复预约
            Integer id = member.getId();
            String setmealId = (String) map.get("setmealId");
            Date order_Date = DateUtils.parseString2Date(orderDate);
            Order order=new Order(id,order_Date,Integer.valueOf(setmealId));
            List<Order> orderList = orderDao.findByCondition(order);
            //如果集合不为空，说明在重复预约
            if(orderList!=null&&orderList.size()>0) return new Result(false,MessageConstant.HAS_ORDERED);
        }else{
            //新用户，自动注册会员
            member=new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);//会员注册
        }
        //预约成功之后，记录当前的预约人信息，当日的预约人数+1
        Order order=new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.valueOf((String) map.get("setmealId")));
        orderDao.add(order);

        //预约设置中的已预约人数+1（单线程下）
        //orderSetting.setReservations(orderSetting.getReservations()+1);
       //orderSettingDao.editReservationsByOrderDate(orderSetting);
        //return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
        //预约设置中的已预约人数+1(在数据库中直接更新数据)
        int count = orderSettingDao.editReservationsByOrderDate2(orderSetting);
        if(count>0)
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
        else return new Result(false,MessageConstant.ORDER_FULL);
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map!=null){
            //把查询的日期转换成字符串在页面进行展示
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
